var seq = 0;
function listSPPResult(sppTable, sppData) {
	$("#sppTable").empty();
	
	if (sppData == null) {
		return;
	}
	
	// 创建收益大于等于零的表格标题
	createSPPTitle(sppTable);
	
	// 初始化小计相关变量为0
	initSubTotalVariable();
	// 初始化总计相关变量为0
	initTotalVariable();
	
	var sppDto;
	seq = 0;
	// 首先创建收益大于等于0的表格
	for (var i = 0; i < sppData.list.length; i++){
		sppDto = sppData.list[i];
		if (sppDto.profit >= 0.0) {
			accumulationSubTotal(sppDto);
			createSppDataBody(sppTable, sppDto);
		}
	}
	// 创建小计行
	createSppDataSubtotal(1, sppTable);
	
	// 累加总计变量
	accumulationTotal();
	
	// 创建空行
	createBlankBR(sppTable, 22);
	
	// 创建收益小于零的表格标题
	createSPPTitle(sppTable);
	seq = 0;
	
	// 初始化小计相关变量为0
	initSubTotalVariable();
	
	// 首先创建收益大于等于0的表格
	for (var i = 0; i < sppData.list.length; i++){
		sppDto = sppData.list[i];
		if (sppDto.profit < 0.0) {
			accumulationSubTotal(sppDto);
			createSppDataBody(sppTable, sppDto);
		}
	}
	// 创建小计行
	createSppDataSubtotal(1, sppTable);
	// 创建空行
	createBlankBR(sppTable, 22);
	
	// 累加总计变量
	accumulationTotal();
	
	// 创建总计行
	createSppDataSubtotal(0, sppTable);
	dealNaN();
}

function creatTD(tr,index, html, align, color, bgcolor) {
	var newTd = tr.insertCell(index);
	newTd.innerHTML = html;
	newTd.setAttribute('align', align);
	if (color) {
		newTd.setAttribute('style', 'color: ' + color);
	}
	if (bgcolor) {
		newTd.setAttribute('bgColor', bgcolor);
	}
	return newTd;
}
/**
 * 创建spp报表的数据部分
 * @param sppTable
 * @param sppDto
 * @returns
 */
function createSppDataBody(sppTable, sppDto) {
	seq++;
	var newTr = sppTable.insertRow(sppTable.rows.length);
	
	// 创建序号单元格
	var newTd = creatTD(newTr, 0, seq, 'center');
	
	var title = "Show performance by league";
	// 创建账号单元格
	var accountHtml = "<a href='javascript:void(0)' title='" + title + "' onclick='openSPPLeagueWindow(\""+ sppDto.accountNumber + "\")' class=\"a_power\">" + 
	sppDto.accountNumber + "</a>";
	newTd = creatTD(newTr, 1, "<strong>" + accountHtml + "</strong>", 'left');
	
	// 创建MP单元格
	var mp = "<a href='javascript:void(0)' onclick='openMPDetail(\""+ sppDto.accountNumber + "\")' class=\"a_power3\">MP</a>";
	newTd = creatTD(newTr, 2, mp, 'center');
	
	// 创建PT%单元格
	newTd = creatTD(newTr, 3, formNumber(sppDto.perQuote,2), 'center');
	
	// 创建Bets单元格
	newTd = creatTD(newTr, 4, sppDto.orderNum, 'center');
	
	// 创建Wins单元格
	newTd = creatTD(newTr, 5, sppDto.winOrder, 'center');
	
	// 创建Lose单元格
	newTd = creatTD(newTr, 6, sppDto.loseOrder, 'center');
	
	// 创建Draw单元格
	newTd = creatTD(newTr, 7, sppDto.drawOrder, 'center');
	
	// 创建Avg Stake单元格
	newTd = creatTD(newTr, 8, formNumber(sppDto.avgItamt, 0), 'right', '#008000');
	
	// 创建Turnover单元格
	newTd = creatTD(newTr, 9, formNumber(sppDto.matchamt, 0), 'right', '#008000');
	
	// 创建W/L单元格
	newTd = creatTD(newTr, 10, formNumber(sppDto.profit, 0), 'right', '#0000FF');
	
	if (sppDto.profit > 0)
		newTd.setAttribute('style', 'color: #0000FF');
	else if (sppDto.profit < 0)
		newTd.setAttribute('style', 'color: #FF0000');
	else 
		newTd.innerHTML = "-";	
	
	// 创建W/L(CNY)单元格
	newTd = creatTD(newTr, 11, formNumber(sppDto.stprofit, 0), 'right', '#0000FF');
	if (sppDto.stprofit > 0)
		newTd.setAttribute('style', 'color: #0000FF');
	else if (sppDto.stprofit < 0)
		newTd.setAttribute('style', 'color: #FF0000');
	else 
		newTd.innerHTML = "-";	
	
	// 创建Win(%)单元格
	newTd = creatTD(newTr, 12, formNumber(sppDto.perRate * 100, 0), 'center', '#0000FF');
	if (sppDto.perRate > 0)
		newTd.setAttribute('style', 'color: #0000FF');
	else if (sppDto.perRate < 0)
		newTd.setAttribute('style', 'color: #FF0000');
	else 
		newTd.innerHTML = "-";	
	
	// 创建BackBet%单元格
	newTd = creatTD(newTr, 13, formNumber(sppDto.buyPerWin * 100, 0), 'center', '#0000FF');
	if (sppDto.buyPerWin == 0)
		newTd.innerHTML = "-";	
	
	// 创建LayBet%单元格
	newTd = creatTD(newTr, 14, formNumber(sppDto.salePerWin * 100, 0), 'center', '#0000FF');
	if (sppDto.salePerWin == 0)
		newTd.innerHTML = "-";	
	
	// 创建Bet%单元格
	newTd = creatTD(newTr, 15, formNumber(sppDto.betPerWin * 100, 0), 'center', '#0000FF');
	if (sppDto.betPerWin == 0)
		newTd.innerHTML = "-";	
	
	// 创建Fav%单元格
	newTd = creatTD(newTr, 16, formNumber(sppDto.upPerWin * 100, 0), 'center', '#0000FF');
	if (sppDto.upPerWin == 0)
		newTd.innerHTML = "-";	
	
	// 创建UD%单元格
	newTd = creatTD(newTr, 17, formNumber(sppDto.downPerWin * 100, 0), 'center', '#0000FF');
	if (sppDto.downPerWin == 0)
		newTd.innerHTML = "-";	
	
	// 创建DNB%单元格
	newTd = creatTD(newTr, 18, formNumber(sppDto.drawPerWin * 100, 0), 'center', '#0000FF');
	if (sppDto.drawPerWin == 0)
		newTd.innerHTML = "-";		
	
	// 创建OV%单元格
	newTd = creatTD(newTr, 19, formNumber(sppDto.bigPerWin * 100, 0), 'center', '#0000FF');
	if (sppDto.bigPerWin == 0)
		newTd.innerHTML = "-";	
	
	// 创建UN%单元格
	newTd = creatTD(newTr, 20, formNumber(sppDto.smallPerWin * 100, 0), 'center', '#0000FF');
	if (sppDto.smallPerWin == 0)
		newTd.innerHTML = "-";	
	
	// 创建CUR单元格
	newTd = newTr.insertCell(21);
	newTd.innerHTML = sppDto.currency;
	newTd.setAttribute('align', 'center');
}

/**
 * 创建spp报表的汇总行，包括小计和总计
 * @param positive 1-大于等于0的表格小计 -1-收益小于0的表格小计 0 总计
 * @param sppTable
 * @param sppDto
 * @returns
 */
function createSppDataSubtotal(positive, sppTable) {

	var newTr = sppTable.insertRow(sppTable.rows.length);
	
	// 创建序号单元格
	var newTd = creatTD(newTr, 0, "<strong>" + "Grand Total in CNY" + "</strong>", 'right', '#000000', '#F5F5DC');
	if (positive !=0) {
		newTd.innerHTML = "<strong>" + "Total Calculated in CNY" + "</strong>";
	} 
	newTd.setAttribute('colspan', '4');
	
	// 创建Bets单元格
	newTd = creatTD(newTr, 1, "<strong>" + (positive == 0 ? totalBets : subTotalBets) + "</strong>", 'center');
	
	// 创建Wins单元格
	newTd = creatTD(newTr, 2, "<strong>" + (positive == 0 ? totalWins : subTotalWins) + "</strong>", 'center');
	
	// 创建Lose单元格
	newTd = creatTD(newTr, 3, "<strong>" + (positive == 0 ? totalLose : subTotalLose) + "</strong>", 'center');
	
	// 创建Draw单元格
	newTd = creatTD(newTr, 4, "<strong>" + (positive == 0 ? totalDraw : subTotalDraw) + "</strong>", 'center');
	
	// 创建Avg Stake单元格
	newTd = creatTD(newTr, 5, "<strong>" + 
			formNumber(positive == 0 ? totalStake / totalBets : subTotalStake / subTotalBets, 0) + 
			"</strong>", 'right', '#008000');
	
	// 创建Turnover单元格
	newTd = creatTD(newTr, 6, "<strong>" + formNumber(positive == 0 ? totalStmatchamt : subTotalStmatchamt, 0) + "</strong>", 
			'right', '#008000');
	
	// 创建W/L单元格
	newTd = creatTD(newTr, 7, "<strong>" + formNumber(positive == 0 ? totalStProfit : subTotalStProfit, 0) + "</strong>", 
			'right', '#0000FF');
	if (positive == 0 ? totalStProfit : subTotalStProfit > 0)
		newTd.setAttribute('style', 'color: #0000FF');
	else if (positive == 0 ? totalStProfit : subTotalStProfit < 0)
		newTd.setAttribute('style', 'color: #FF0000');
	
	// 创建W/L(CNY)单元格
	newTd = creatTD(newTr, 8, "<strong>" + formNumber(positive == 0 ? totalStProfit : subTotalStProfit, 0) + "</strong>", 
			'right', '#0000FF');
	if (positive == 0 ? totalStProfit : subTotalStProfit > 0)
		newTd.setAttribute('style', 'color: #0000FF');
	else if (positive == 0 ? totalStProfit : subTotalStProfit < 0)
		newTd.setAttribute('style', 'color: #FF0000');
	
	var tmpTotalStProfit = positive == 0 ? totalStProfit : subTotalStProfit;
	var tmpTotalStmatchamt = positive == 0 ? totalStmatchamt : subTotalStmatchamt; 
	// 创建Win(%)单元格
	newTd = creatTD(newTr, 9, "<strong>" + formNumber(tmpTotalStProfit*100 / tmpTotalStmatchamt, 0) + "</strong>", 
			'center', '#0000FF');
	if (tmpTotalStProfit > 0)
		newTd.setAttribute('style', 'color: #0000FF');
	else if (tmpTotalStProfit < 0)
		newTd.setAttribute('style', 'color: #FF0000');
	
	// 创建BackBet%单元格
	newTd = creatTD(newTr, 10, "<strong>" + formNumber(positive == 0 ? totalBuyWinOrders * 100 / totalBuyOrders : subTotalBuyWinOrders * 100 / subTotalBuyOrders, 0) + "</strong>", 
			'center', '#0000FF');
	
	// 创建LayBet%单元格
	newTd = creatTD(newTr, 11, "<strong>" + formNumber(positive == 0 ? totalLayWinOrders * 100 / totalLayOrders : subTotalLayWinOrders * 100 / subTotalLayOrders, 0) + "</strong>", 
			'center', '#0000FF');
	
	// 创建Bet%单元格
	newTd = creatTD(newTr, 12, "<strong>" + formNumber(positive == 0 ? totalWins * 100 / totalBets : subTotalWins * 100 / subTotalBets, 0) + "</strong>", 
			'center', '#0000FF');
	
	// 创建Fav%单元格
	newTd = creatTD(newTr, 13, "<strong>" + formNumber(positive == 0 ? totalUpWinOrders * 100 / totalUpOrders : subTotalUpWinOrders * 100 / subTotalUpOrders, 0) + "</strong>", 
			'center', '#0000FF');
	
	// 创建UD%单元格
	newTd = creatTD(newTr, 14, "<strong>" + formNumber(positive == 0 ? totalDownWinOrders * 100 / totalDownOrders : subTotalDownWinOrders * 100 / subTotalDownOrders, 0) + "</strong>", 
			'center', '#0000FF');
	
	// 创建DNB%单元格
	newTd = creatTD(newTr, 15, "<strong>" + formNumber(positive == 0 ? totalDrawWinOrders * 100 / totalDrawOrders : subTotalDrawWinOrders * 100 / subTotalDrawOrders, 0) + "</strong>", 
			'center', '#0000FF');
	if (newTd.innerHTML.indexOf('NaN') != -1) {
		newTd.innerHTML = '-';
	}
		
	
	// 创建OV%单元格
	newTd = creatTD(newTr, 16, "<strong>" + formNumber(positive == 0 ? totalBigWinOrders * 100 / totalBigOrders : subTotalBigWinOrders * 100 / subTotalBigOrders, 0) + "</strong>", 
			'center', '#0000FF');
	
	// 创建UN%单元格
	newTd = creatTD(newTr, 17, "<strong>" + formNumber(positive == 0 ? totalSmallWinOrders * 100 / totalSmallOrders : subTotalSmallWinOrders * 100 / subTotalSmallOrders, 0) + "</strong>", 
			'center', '#0000FF');
	
	// 创建CUR单元格
	newTd = newTr.insertCell(18);
	newTd.innerHTML = " ";
	newTd.setAttribute('align', 'center');
}

function createSPPTitle(sppTable) {
	// 创建行
	var newTr = sppTable.insertRow(sppTable.rows.length);
	newTr.setAttribute('bgColor', '#99BBFF');
	
	var newTd = creatTD(newTr, 0, "<strong>" + "#" + "</strong>", 'center');
	newTd.setAttribute('width', "20");
	newTd = creatTD(newTr, 1, "<strong>" + "No." + "</strong>", 'center');
	newTd.setAttribute('width', "100");
	newTd = creatTD(newTr, 2, "<strong>" + "MP" + "</strong>", 'center');
	newTd = creatTD(newTr, 3, "<strong>" + "PT%" + "</strong>", 'center');
	newTd = creatTD(newTr, 4, "<strong>" + "Bets" + "</strong>", 'center');
	newTd = creatTD(newTr, 5, "<strong>" + "Wins" + "</strong>", 'center');
	newTd = creatTD(newTr, 6, "<strong>" + "Lose" + "</strong>", 'center');
	newTd = creatTD(newTr, 7, "<strong>" + "Draw" + "</strong>", 'center');
	newTd = creatTD(newTr, 8, "<strong>" + "Avg Stake" + "</strong>", 'center');
	newTd = creatTD(newTr, 9, "<strong>" + "Turnover" + "</strong>", 'center');
	newTd = creatTD(newTr, 10, "<strong>" + "W/L" + "</strong>", 'center');
	newTd = creatTD(newTr, 11, "<strong>" + "W/L(CNY)" + "</strong>", 'center');
	newTd = creatTD(newTr, 12, "<strong>" + "Win%" + "</strong>", 'center');
	newTd = creatTD(newTr, 13, "<strong>" + "Back%" + "</strong>", 'center');
	newTd = creatTD(newTr, 14, "<strong>" + "Lay%" + "</strong>", 'center');
	newTd = creatTD(newTr, 15, "<strong>" + "Bet%" + "</strong>", 'center');
	newTd = creatTD(newTr, 16, "<strong>" + "Fav%" + "</strong>", 'center');
	newTd = creatTD(newTr, 17, "<strong>" + "UD%" + "</strong>", 'center');
	newTd = creatTD(newTr, 18, "<strong>" + "DNB%" + "</strong>", 'center');
	newTd = creatTD(newTr, 19, "<strong>" + "OV%" + "</strong>", 'center');
	newTd = creatTD(newTr, 20, "<strong>" + "UN%" + "</strong>", 'center');
	newTd = creatTD(newTr, 21, "<strong>" + "CUR" + "</strong>", 'center');
}

//数字填充，为了提高处理性能定义的填充字符串，如果填充更多只需要修改这个变量即可
var fillNum = "0000000000"; //注：为了不影响不被替换，建议定义为对象属性。
		    //jquery.sgfmtable.js插件中可通过在页面定义 sgfm.fillNum 的属性进行替换。
//格式化数字111,111.00 ，不是数字类型不进行格式化,只支持16位精度
// 参数str:需要格式化的数字，不是数字类型不进行格式化
// 参数num:小数点保留多少位，不是数字或不传为默认保留两位
function formNumber(str,num){
	if(!isNaN(str) && $.trim(str) !==""){
	var html = str.toString(),digits=num,prefix=str<0?"-":"";
	if(!(/^[\d]+$/.test(digits))){
		digits = 2;
	}
	var pow10 = Math.pow(10,digits);
	html = Math.abs(Math.round(parseFloat(html)*pow10)/pow10).toString();
	var index = html.indexOf("."),sDigits="",length=html.length;
	if(index===-1){index=length++;sDigits=".";}
	if(digits>0){
		var inum = digits-(length-index-1);
		if(inum>0){
			sDigits += fillNum.substr(0,inum);//不足保留小数位补0 ,“fillNum”要与定义的变量相同
			html+=sDigits;
		}
	}
	if(index===0){
		html = "0"+ html;
	}else if(index > 3){
		var inum = Math.ceil(index/3)-1,text=html.substr(index-3),tmpText=html.substring(0,index-3);
		for(var i=1;i<inum;i++){
			var tmpLength=tmpText.length;
			text = tmpText.substr(tmpLength-3) + text;
			//text = tmpText.substr(tmpLength-3) + "," + text;
			tmpText = tmpText.substring(0,tmpLength-3);
		}
		html = tmpText+text;
		//html = tmpText+","+text;
		}
		return prefix+html;
	}
	return str;
}

/**
 * 创建表空行
 * @param table
 * @param colspan
 * @returns
 */
function createBlankBR(table, colspan) {
	var newTr = table.insertRow(table.rows.length);
	
	// 创建单元格
	var newTd = newTr.insertCell(0);
	newTd.innerHTML = "&nbsp;";
	newTd.setAttribute('colspan', colspan);
}

/**
 * subTotalBets 注单数小计
 * subTotalWins 注单数（赢单）小计
 * subTotalLose 注单数（输单）小计
 * subTotalDraw 注单数（平单）小计
 * 
 * subTotalStake 标准币投注额度小计
 * 
 * subTotalStmatchamt 标准币成交额度小计
 * 
 * subTotalAvgStake 标准币平均投注额度小计
 * 
 * subTotalStProfit 标准币收益小计
 * 
 */
var subTotalBets, subTotalWins, subTotalLose, subTotalDraw, 
	subTotalBuyOrders, subTotalBuyWinOrders, subTotalLayOrders, subTotalLayWinOrders,
	subTotalUpOrders, subTotalUpWinOrders, subTotalDownOrders, subTotalDownWinOrders,
	subTotalDrawOrders, subTotalDrawWinOrders, subTotalBigOrders, subTotalBigWinOrders,
	subTotalSmallOrders, subTotalSmallWinOrders, subTotalStake,	subTotalStProfit, subTotalStmatchamt;
var totalBets = 0, totalWins = 0, totalLose = 0, totalDraw = 0, 
	totalBuyOrders = 0, totalBuyWinOrders = 0, totalLayOrders = 0, totalLayWinOrders = 0,
	totalUpOrders = 0, totalUpWinOrders = 0, totalDownOrders = 0, totalDownWinOrders = 0,
	totalDrawOrders = 0, totalDrawWinOrders = 0, totalBigOrders = 0, totalBigWinOrders = 0,
	totalSmallOrders = 0, totalSmallWinOrders = 0,
	totalStake = 0, totalStProfit = 0, totalStmatchamt = 0;

/**
 * 初始化小计相关变量
 */
function initSubTotalVariable() {
	subTotalBets = 0; subTotalWins = 0;	subTotalLose = 0; subTotalDraw = 0;
	subTotalBuyOrders = 0; subTotalBuyWinOrders = 0;
	subTotalLayOrders = 0; subTotalLayWinOrders = 0;
	subTotalUpOrders = 0; subTotalUpWinOrders = 0; 
	subTotalDownOrders = 0; subTotalDownWinOrders = 0; 
	subTotalDrawOrders = 0;	subTotalDrawWinOrders = 0; 
	subTotalBigOrders = 0; subTotalBigWinOrders = 0; 
	subTotalSmallOrders = 0; subTotalSmallWinOrders = 0; 
	subTotalStake = 0; subTotalStProfit = 0;subTotalStmatchamt = 0;	
}
/**
 * 初始化总计相关变量
 */
function initTotalVariable() {
	totalBets = 0; totalWins = 0; totalLose = 0; totalDraw = 0; 
	totalBuyOrders = 0; totalBuyWinOrders = 0; totalLayOrders = 0; totalLayWinOrders = 0;
	totalUpOrders = 0; totalUpWinOrders = 0; totalDownOrders = 0; totalDownWinOrders = 0;
	totalDrawOrders = 0; totalDrawWinOrders = 0; totalBigOrders = 0; totalBigWinOrders = 0;
	totalSmallOrders = 0; totalSmallWinOrders = 0;
	totalStake = 0; totalStProfit = 0; totalStmatchamt = 0;
}

/**
 * 累加小计相关变量
 * @param sppDto 数据对象
 */
function accumulationSubTotal(sppDto) {
	subTotalBets += sppDto.orderNum;
	subTotalWins += sppDto.winOrder;
	subTotalLose += sppDto.loseOrder;
	subTotalDraw += sppDto.drawOrder;
	
	subTotalBuyOrders += sppDto.buyOrders;
	subTotalBuyWinOrders += sppDto.buyWinOrders;
	subTotalLayOrders += sppDto.layOrders;
	subTotalLayWinOrders += sppDto.layWinOrders;
	subTotalUpOrders += sppDto.upOrders;
	subTotalUpWinOrders += sppDto.upWinOrders;
	subTotalDownOrders += sppDto.downOrders; 
	subTotalDownWinOrders += sppDto.downWinOrders; 
	subTotalDrawOrders += sppDto.drawOrders;  
	subTotalDrawWinOrders += sppDto.drawWinOrders;  
	subTotalBigOrders += sppDto.bigOrders;  
	subTotalBigWinOrders += sppDto.bigWinOrders;  
	subTotalSmallOrders += sppDto.smallOrders;  
	subTotalSmallWinOrders += sppDto.smallWinOrders;  
	
	subTotalStake += sppDto.stamt;
	subTotalStProfit += sppDto.stprofit;
	
	subTotalStmatchamt += sppDto.stmatchamt;	
}

/**
 * 将小计累加到总计上
 */
function accumulationTotal() {
	totalBets += subTotalBets;
	totalWins += subTotalWins;
	totalLose += subTotalLose;
	totalDraw += subTotalDraw;
	totalBuyOrders += subTotalBuyOrders;
	totalBuyWinOrders += subTotalBuyWinOrders;
	totalLayOrders += subTotalLayOrders;
	totalLayWinOrders += subTotalLayWinOrders;

	totalUpOrders += subTotalUpOrders;
	totalUpWinOrders += subTotalUpWinOrders;
	totalDownOrders += subTotalDownOrders; 
	totalDownWinOrders += subTotalDownWinOrders; 
	totalDrawOrders += subTotalDrawOrders;  
	totalDrawWinOrders += subTotalDrawWinOrders;
	totalBigOrders += subTotalBigOrders;  
	totalBigWinOrders += subTotalBigWinOrders;  
	totalSmallOrders += subTotalSmallOrders;  
	totalSmallWinOrders += subTotalSmallWinOrders;
	
	totalStake += subTotalStake;
	totalStmatchamt += subTotalStmatchamt;
	totalStProfit += subTotalStProfit;
}

function dealNaN() {
	$("#sppTable td").each(function(){
	    if ($(this).html().indexOf('NaN') != -1) {
	    	$(this).html('-');
	    }

	})
}